from sqlalchemy import Column, Integer, String, Text, JSON, Index
from app.db.base import Base

class Question(Base):
    """题目数据库模型"""
    __tablename__ = "questions"

    # 主键
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    
    # 基本信息
    subject = Column(String(50), nullable=False, comment="考试科目 (k1: 科目一, k4: 科目四)")
    model = Column(String(50), nullable=False, comment="车辆类型 (C1: 轿车, A2: 货车, D: 摩托车, A1: 公交车)")
    question_type = Column(String(50), nullable=False, comment="题目类型 (theory: 理论考试, special: 专项训练)")
    
    # 题目内容
    content = Column(Text, nullable=False, comment="题目内容")
    options = Column(JSON, nullable=False, comment="选项列表，JSON格式")
    answer = Column(String(10), nullable=False, comment="正确答案")
    explanation = Column(Text, nullable=True, comment="答案解释")
    image_url = Column(String(255), nullable=True, comment="题目图片URL")

    # 创建复合索引
    __table_args__ = (
        Index('idx_subject_model', 'subject', 'model'),  # 用于按科目和车型查询
        Index('idx_question_type', 'question_type'),     # 用于按题目类型查询
        {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_unicode_ci'}
    )

    def __repr__(self):
        return f"<Question(id={self.id}, subject={self.subject}, model={self.model})>"
    